<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="wishList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="标题" prop="title"/>
      <el-table-column align="center" label="访问人数" prop="visitNum"/>
      <el-table-column align="center" label="活动名额" prop="totalPeople"/>
      <el-table-column align="center" label="活动地址" prop="activityPlace"/>
      <el-table-column align="center" label="封面" prop="cover">
        <template slot-scope="scope">
          <img :src="scope.row.cover" alt="" width="100px" height="100px">
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.shelfStatus"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <!--<el-table-column label="报名开始时间" align="center" prop="applyStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名结束时间" align="center" prop="applyEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改心愿 活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题" prop="title">
              <el-input placeholder="请输入标题" v-model="form.title"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="上架状态">
              <el-radio-group v-model="form.shelfStatus">
                <el-radio
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                  v-for="dict in statusOptions"
                >{{dict.dictLabel}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="置顶状态">
              <el-radio-group v-model="form.topStatus">
                <el-radio
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                  v-for="dict in statusTopOptions"
                >{{dict.dictLabel}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="活动类型">
              <el-radio-group v-model="form.activityType">
                <el-radio
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                  v-for="dict in activityTypeOptions"
                >{{dict.dictLabel}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="活动地址" prop="activityPlace">
              <el-input placeholder="请输入活动地址" v-model="form.activityPlace"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="活动名额" prop="activityPlace">
              <el-input placeholder="请输入活动名额" v-model="form.totalPeople"/>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="封面" prop="cover">
              <el-upload
                :action="action"
                :on-success="uploadSuccess"
                :show-file-list="false"
                class="avatar-uploader"
              >
                <img :src="form.cover" class="avatar" v-if="form.cover"/>
                <i class="el-icon-plus avatar-uploader-icon" v-else></i>
              </el-upload>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="报名开始" prop="applyStartTime">
              <el-date-picker clearable placeholder="选择报名开始时间" size="small"
                              style="width: 200px"
                              type="date"
                              v-model="form.applyStartTime"
                              value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="报名结束" prop="applyEndTime">
              <el-date-picker clearable placeholder="选择报名结束时间" size="small"
                              style="width: 200px"
                              type="date"
                              v-model="form.applyEndTime"
                              value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="活动开始" prop="startTime">
              <el-date-picker clearable placeholder="选择开始时间" size="small"
                              style="width: 200px"
                              type="datetime"
                              v-model="form.startTime"
                              value-format="yyyy-MM-dd HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="活动结束" prop="endTime">
              <el-date-picker clearable placeholder="选择结束时间" size="small"
                              style="width: 200px"
                              type="datetime"
                              v-model="form.endTime"
                              value-format="yyyy-MM-dd HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="描述" prop="description">
              <Editor v-model="form.description"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="dialog-footer" slot="footer" style="padding-top:20px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listWish, getWish, delWish, addWish, updateWish, exportWish} from "@/api/system/wish";
  import Editor from '@/components/Editor'

  export default {
    name: "Wish",
    components: {
      Editor
    },
    data() {
      return {
        isAdd: true,
        action: process.env.VUE_APP_BASE_API + '/system/upload/file',
        statusOptions: [],
        statusTopOptions: [],
        activityTypeOptions: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 心愿 活动表格数据
        wishList: [],
        // 弹出层标题
        title: "",
        activityType: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: undefined,
          type: 2,
          shelfStatus: undefined,
          showStatus: undefined,
          topStatus: undefined,
          visitNum: undefined,
          totalPeople: undefined,
          activityPlace: undefined,
          cover: undefined,
          applyStartTime: undefined,
          applyEndTime: undefined,
          startTime: undefined,
          endTime: undefined,
          description: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          title: [
            {required: true, message: "标题不能为空", trigger: "blur"}
          ],
          type: [
            {required: true, message: "1-wish 2-party不能为空", trigger: "blur"}
          ],
          shelfStatus: [
            {required: true, message: "上架状态，0：未上架  1：已上架不能为空", trigger: "blur"}
          ],
          showStatus: [
            {required: true, message: "栏目展示状态 0：不展示  1：展示不能为空", trigger: "blur"}
          ],
          topStatus: [
            {required: true, message: "置顶状态", trigger: "blur"}
          ],
          visitNum: [
            {required: true, message: "访问人数不能为空", trigger: "blur"}
          ],
          totalPeople: [
            {required: true, message: "活动人数不能为空", trigger: "blur"}
          ],
          activityPlace: [
            {required: true, message: "活动地址不能为空", trigger: "blur"}
          ],
          cover: [
            {required: true, message: "封面不能为空", trigger: "blur"}
          ],
          applyStartTime: [
            {required: true, message: "报名开始时间不能为空", trigger: "blur"}
          ],
          applyEndTime: [
            {required: true, message: "报名结束时间不能为空", trigger: "blur"}
          ],
          startTime: [
            {required: true, message: "开始时间不能为空", trigger: "blur"}
          ],
          endTime: [
            {required: true, message: "结束时间不能为空", trigger: "blur"}
          ],
          createTime: [
            {required: true, message: "创建时间不能为空", trigger: "blur"}
          ],
          updateTime: [
            {required: true, message: "更新时间不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getDicts('sys_shelf_status').then(response => {
        this.statusOptions = response.data
      })
      this.getDicts('sys_top_status').then(response => {
        this.statusTopOptions = response.data
      })
      this.getDicts('sys_activity_type').then(response => {
        this.activityTypeOptions = response.data
      })
    },
    methods: {
      handleStatusChange(row) {
        let text = row.shelfStatus == '0' ? '下架' : '上架'
        this.$confirm('确认要"' + text + '"活动吗?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          let data = {
            'id': row.id,
            'shelfStatus': row.shelfStatus
          }
          return updateWish(data)
        }).then(() => {
          this.msgSuccess(text + '成功')
        }).catch(function () {
          row.status = row.status == '0' ? '1' : '0'
        })
      },
      uploadSuccess(e) {
        console.log(e)
        if (e.code === 200) {
          this.form.cover = e.data.path
        }
      },
      /** 查询心愿 活动列表 */
      getList() {
        this.loading = true;
        listWish(this.queryParams).then(response => {
          this.wishList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          title: undefined,
          type: undefined,
          shelfStatus: "0",
          showStatus: "0",
          topStatus: "0",
          visitNum: undefined,
          totalPeople: undefined,
          activityPlace: undefined,
          cover: undefined,
          applyStartTime: undefined,
          applyEndTime: undefined,
          startTime: undefined,
          endTime: undefined,
          description: undefined,
          createTime: undefined,
          updateTime: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.isAdd = true
        this.open = true;
        this.title = "添加心愿 活动";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.isAdd = false
        const id = row.id || this.ids
        getWish(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改心愿 活动";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          console.log('表单提交数据', this.form)
          // return
          if (valid) {
            this.form.type = 2
            if (this.form.id != undefined) {
              this.isAdd = false
              updateWish(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              this.isAdd = true
              addWish(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除心愿 活动编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delWish(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有心愿 活动数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportWish(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
